package com.hjjt.baselib.util

import android.content.Context
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.style.ForegroundColorSpan
import android.widget.TextView

/**
 *
 * @ProjectName:    IntelligentTerminal
 * @Package:        com.hjjt.baselib.util
 * @ClassName:      UtilBox
 * @Description:
 * @Author:         孙浩
 * @CreateDate:     2022/9/28 9:23
 */
object UtilBox {
    fun highLightString(
        totalString: String,
        color: Int,
        vararg highLightString: String

    ): SpannableStringBuilder {
        var style =
            SpannableStringBuilder(totalString)
        highLightString.forEach {
            var startIndex = style.indexOf(it)
            style.apply {
                setSpan(
                    ForegroundColorSpan(color),
                    startIndex,
                    startIndex + it.length,
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
                )
            }
        }
        return style
    }

    fun getVersionName(context:Context):String {
        return try {
            val manager: PackageManager = context.packageManager
            val info: PackageInfo =
                manager.getPackageInfo(context.packageName, 0) //PackageManager.GET_CONFIGURATIONS
            info.versionName
        } catch (e: Exception) {
            e.printStackTrace()
            ""
        }
    }

}

fun Context.getAppVersionName():String{
    return UtilBox.getVersionName(this)
}

fun TextView.highLightString(totalString: String, color: Int, vararg highLightString: String) {
    text = UtilBox.highLightString(totalString, color, *highLightString)
}